package com.newcoder.controller;

import com.newcoder.entity.User;
import com.newcoder.service.impl.UserServiceImpl;
import com.newcoder.util.ResponseWrapper;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yjh
 * @since 2024-12-04 10:03:26
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserServiceImpl userService;

    @ApiOperation("新增用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "user",value = "用户表单"),
    })
    @PostMapping("/insertUser")
    public ResponseWrapper insertUser(@RequestBody User user){
        ResponseWrapper wrapper = userService.insertUser(user);
        return wrapper;
    }

    @ApiOperation("删除用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "用户id"),
    })
    @GetMapping("/deleteUserById")
    public ResponseWrapper deleteUserById(@RequestParam(name = "id")Integer id){
        ResponseWrapper wrapper = userService.deleteUserById(id);
        return wrapper;
    }

    @ApiOperation("修改用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "user",value = "用户表单"),
    })
    @PostMapping("/updateUserById")
    public ResponseWrapper updateUserById(@RequestBody User user){
        ResponseWrapper wrapper = userService.updateUserById(user);
        return wrapper;
    }

    @ApiOperation("修改用户头像")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "用户id"),
            @ApiImplicitParam(name = "headerImage",value = "头像图片"),
    })
    @PostMapping("/updateHeader")
    public ResponseWrapper updateHeader(@RequestParam(name = "id")Integer id, @RequestParam(name = "headerImage") MultipartFile headerImage){
        ResponseWrapper wrapper = userService.updateHeader(id, headerImage);
        return wrapper;
    }

    @ApiOperation("根据id查找用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "用户id"),
    })
    @GetMapping("/getUserById")
    public ResponseWrapper getUserById(@RequestParam(name = "id")Integer id){
        ResponseWrapper wrapper = userService.getUserById(id);
        return wrapper;
    }

    @ApiOperation("根据username查找用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username",value = "用户名"),
    })
    @GetMapping("/getUserByName")
    public ResponseWrapper getUserByName(@RequestParam(name = "username")String username){
        ResponseWrapper wrapper = userService.getUserByName(username);
        return wrapper;
    }

    @ApiOperation("查询某一页用户数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "status", defaultValue = "null"),
            @ApiImplicitParam(name = "type", defaultValue = "null"),
            @ApiImplicitParam(name = "currentPage",value = "当前页码数",defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize",value = "每页展示的数据条数",defaultValue = "10"),
    })
    @GetMapping("/selectUserPage")
    public ResponseWrapper selectUserPage(@RequestParam(name = "status",defaultValue = "123")Integer status,
                                          @RequestParam(name = "type",defaultValue = "123")Integer type,
                                          @RequestParam(name = "currentPage",defaultValue = "1")Integer currentPage,
                                          @RequestParam(name = "pageSize",defaultValue = "10")Integer pageSize){
        ResponseWrapper wrapper = userService.selectUserPage(status, type, currentPage, pageSize);
        return wrapper;
    }
}
